//
//  CamlController.m
//  CocOCaml
//
//	$Id: CamlController.m,v 1.2 2003/02/27 17:35:32 wneuman Exp $
//
//  Created by William D. Neumann on Thu Feb 13 2003.
//  Copyright (c) 2003 William D. Neumann. All rights reserved.
//
//	This program is free software; you can redistribute it and/or modify
//	it under the terms of the Artistic License as published by Larry
//	Wall, either version 2.0, or (at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	Artistic License for more details.
//
//	You should have received a copy of the Artistic License with this Kit,
//	in the file named "Artistic.clarified".  If not, I'll be glad to provide
//	one.
//

#import "CamlController.h"
#import "PreferenceController.h"

@implementation CamlController

-(id) init
{
    return [self initWithOwner:nil];
}

-(id) initWithOwner:(id)anOwner
{
    NSMutableArray * taskArgs = [NSMutableArray arrayWithObject:
        [[NSUserDefaults standardUserDefaults] objectForKey:WDNInterpreterPathKey]];
    NSArray * argList = [[[NSUserDefaults standardUserDefaults] objectForKey:WDNInterpreterArgsKey]
                            componentsSeparatedByString:@" "];
    NSEnumerator * argEnumerator = [argList objectEnumerator];
    NSString * currentArg;
    while (currentArg = [argEnumerator nextObject]) {
        if (![currentArg isEqualToString:@""]) [taskArgs addObject:currentArg];
    }
    if (self = [super init]) {
        owner = anOwner;
        camlTask = [[InteractiveTaskWrapper alloc] initWithController:self
                                                            arguments:taskArgs];
        [camlTask startProcess];
    }
    return self;
}

- (void)appendOutput:(NSString *)output
{
    //	Send the output back to the document and let it handle the updating
    [owner updateCamlView:output];
}

- (void)processStarted
{
    interpreterRunning = YES;
}

- (void)processFinished
{
    interpreterRunning = NO;
    [NSApp terminate:nil];
}

- (void)sendDataToTask:(NSData *)theData
{
    [camlTask sendData:theData];
}

- (BOOL)taskIsRunning
{
    return interpreterRunning;
}

- (void)interruptInterpreter
{
    [camlTask interruptTask];
}

@end
